﻿using System;
using System.Collections.Generic;
using System.Web;
using Wind.MultimediaStation.WebService.Entity;
using Wind.Speed;
using Wind.Utils;
using System.Collections;
using System.Data;
using Wind.Utils.DataAccess;
using Wind.Framework.Config;

namespace Wind.MultimediaStation.WebService
{
    /// <summary>
    /// 单个分时图
    /// </summary>
    public class MultimediaStationChartBusiness
    {
        private static IDBHelper _dbHelper = DBFactoryHelper.GetDatabase(DatabaseType.SqlServer, FrameworkSection.GetConnectionString("ConnectionString"));
        private const string SP_GetMarketTimeInfoByWindcode = "SP_MultimediaStation_GetMarketTimeInfoByWindcode";
        /// <summary>
        ///分时图数据
        /// </summary>
        /// <param name="Windcode">WindCode</param>
        /// <returns></returns>
        public static StockQuote QueryMaterialStockQuoteListByWindcode(string Windcode)
        {
            try
            {

                string OpenMarket = "09:30";
                string MidCloseMarket = "11:30";
                string MidOpenMarket = "13:00";
                string CloseMarket = "15:00";
                Hashtable ht = new Hashtable();
                ht.Add("@MaterialCode", Windcode);
                DataTable dt = _dbHelper.ExcuteDataTable(SP_GetMarketTimeInfoByWindcode, ht);
                if (dt != null && dt.Rows.Count > 0)
                {
                     OpenMarket=dt.Rows[0]["OpenMarket"].ToString().Trim();
                     MidCloseMarket=dt.Rows[0]["MidCloseMarket"].ToString().Trim();
                     MidOpenMarket=dt.Rows[0]["MidOpenMarket"].ToString().Trim();
                     CloseMarket=dt.Rows[0]["CloseMarket"].ToString().Trim();
                }
                StockQuote quote = new StockQuote(DateTime.Parse(OpenMarket), DateTime.Parse(MidCloseMarket), DateTime.Parse(MidOpenMarket), DateTime.Parse(CloseMarket));
                quote.QuoteDateList = new List<string>();
                quote.QuotePriceList = new List<double>();
                quote.Windcode = Windcode;
                MultimediaStationSpeedSharpBusiness msssb = new MultimediaStationSpeedSharpBusiness();
                //实时行情
                DataLines data = msssb.GetSectorMDSnapshot(Windcode);
                if (data.ErrorCode == SpeedErrors.NO_ERROR)
                {
                    string strCurrentDate = data.Items[0].Values[1].ToString() + data.Items[0].Values[2].ToString().PadLeft(6, '0');
                    quote.CurrentDate = strCurrentDate.Substring(8, 2) + ":" + strCurrentDate.Substring(10, 2);
                    quote.CurrentPrice = double.Parse(data.Items[0].Values[3].ToString()).ToString("f2");
                    quote.Swing =double.Parse(data.Items[0].Values[8].ToString()).ToString("f2");
                    double Swing = double.Parse(quote.Swing);
                    quote.SwingPercent =(double.Parse(data.Items[0].Values[9].ToString()) * 100).ToString("f2") + "%";
                    if (Swing>=0)
                    {
                        quote.SwingPercent = "+" + quote.SwingPercent;
                    }
                    else
                    {
                        quote.IsDown = true;
                    }
                    quote.YestodayPrice = double.Parse(data.Items[0].Values[4].ToString());
                }
                //分时数据
                uint StartTime=uint.Parse(quote.QuoteStartTime.ToString("HHmmss"));
                TrendIntraDayData tidd = msssb.GetTrendIntraDayData(Windcode,StartTime);
               // LogHelper.Write("MultimediaStationChartBusiness QueryMaterialStockQuoteListByWindcode info:"+tidd.ErrorCode.ToString());
                if (tidd.ErrorCode == SpeedErrors.NO_ERROR)
                {
                    foreach (uint t in tidd.TrendTime)
                    {
                        string strTime = t.ToString().PadLeft(6, '0');
                        strTime = strTime.Substring(0, 2) + ":" + strTime.Substring(2, 2);
                        quote.QuoteDateList.Add(strTime);
                    }
                    double[] tData = tidd.Lines[0].Data;
                    foreach (double d in tData)
                    {
                        quote.QuotePriceList.Add(d);
                    }
                }               
                return quote;
            }
            catch (Exception ex)
            {
                LogHelper.Write("MultimediaStationChartBusiness QueryMaterialStockQuoteListByWindcode Exception:", ex);
                return null;
            }
        }       
    }
}
